package com.link.base.base.errorcode.controller;

import com.link.base.base.errorcode.model.ErrorCode;
import com.link.base.base.errorcode.service.ErrorCodeService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 错误编码维护
 *
 * @author cuizhiyong
 * @date 2018/11/12 9:46
 */

@RestController
@RequestMapping("/link/errorCode")
public class ErrorCodeController extends BasicController<ErrorCode> {

    @Resource
    private ErrorCodeService errorCodeService;

    @Override
    public BasicService<ErrorCode> getBasicService() throws Exception {
        return errorCodeService;
    }

    @Override
    public void beforUpsert(ErrorCode entity, HttpServletRequest request) throws Exception {
        if (StringUtils.isBlank(entity.getErrorCode())) {
            throw new ServiceException("ERROR-001");
        }
        if (StringUtils.isBlank(entity.getErrorLevel())) {
            throw new ServiceException("ERROR-002");
        }
        if (errorCodeService.duplicateValidate(entity)) {
            throw new ServiceException("ERROR-003");
        }
    }
}
